# crm 直播课英文用户报班 批量下单并审核
import time

import requests

# ---------------------------配置信息start，根据需要配置crm_online、classCode、username1、username2、mobile_lst--------------
# 是否生产环境， True or False
# crm_online = False
crm_online = True

# 班级编码
classCode = ""

# 测试环境下单token
token = ""
# 测试环境审核订单token
check_token = ""

# 生产环境下单账号密码参数, 13240480333
username1 = {
    "account": "8YFixT7qY49oDXc0vidXTQ==",
    "authCode": "UlBw5DbsCJJwffjX+ykjPA==",
    "type": 1
}
# 生产环境审核订单账号密码参数, 15811326167
username2 = {
    "account": "nX0ffoW2IyFuYG5Bo2USRg==",
    "authCode": "rE3/GLAaIiJwffjX+ykjPA==",
    "type": 1
}

# 测试域名
domain = "oapi.t.blingabc.com"
# 生产域名
p_domain = "oapi.blingabc.com"

# 用户手机号，下单用户手机号--自调整
mobile_lst = [
    13240480333,
    17988880001,
    17988880002,
    17988880003,
    17988880004,
    17988880005,
    17988880006,
    17988880007,
    17988880008,
    17988880009,

    17988880014,
    17988880015,
    17988880016,
    17988880017,
    17988880018,
    17988880019,
    17988880021,
    17988880022,
    17988880023,
    17988880024,
    17988880025,
    17988880026,
    17988880027,
    17988880028,
    17988880029,
    17988880030,
    17988880031,
    17988880032,
    17988880033,
    17988880034,
    17988880035,
    17988880036,
    17988880037,
    17988880038,
    17988880039,
    17988880041,
    17988880042,
    17988880043,
    17988880044,
    17988880051,
    17988880052,

    18515380719,
    15811326167,
    15838643134,
    ]

# ---------------------------配置信息end--------------------------------------------------------------------------------


# 初始化
def initialize():
    global domain, p_domain, token, check_token
    # 测试环境账号 下单账号crmtestadmin
    order_login_body = {"account": "WJ2tod/VMtdwffjX+ykjPA==", "authCode": "6KbJUUHSdPLW8Gpw27QWlQ==", "type": 1}
    # 审核账号crmadmin
    check_login_body = {"account": "+CYQ0v7PAfWjeQv9RDUZQg==", "authCode": "8x5rTYjUtRk=", "type": 1}
    # 生产环境账号
    if crm_online:
        order_login_body = username1
        check_login_body = username2
        domain = p_domain
        login_url = "https://lapi.blingabc.com/auth/open-api/login/v1/login"
    # login_url = "https://{}/auth/open-api/login/v1/login".format(domain)
    else:
        login_url = "https://lapi.t.blingabc.com/auth/open-api/login/v1/login"
    try:
        r1 = requests.post(url=login_url, json=order_login_body)
        r1_token = r1.json()['data']['token']
        print(r1_token)
        r2 = requests.post(url=login_url, json=check_login_body)
        r2_token = r2.json()['data']['token']
        print(r2_token)
    except Exception as e:
        print("登录失败：", e)
        return

    token = r1_token
    check_token = r2_token


# 初始化--判断环境、登录、获取token
initialize()

# 下单账号请求头
headers = {
    "Content-Type": "application/json",
    "token": token
}

# 审核账号请求头
check_headers = {
    "token": check_token
}

# 提交订单参数
body = {
    "orderChannel": 11,
    "classList": [
        {
            "id": 505729,
            "classCode": classCode
        }
    ],
    "createId": 1983,
    "stuNum": "731726841",
    "userCode": "crmtestadmin",
    "discountId": "",
    "useCouponCode": [],
    "sendCouponCodes": [],
    "courseFavorableState": 1,
    "remark": "",
    "isFreePayment": 0,
    "orderTotalPrice": 0,
    "orderMaterialsPrice": 0,
    "checkFlage": 1,
    "couponList": []
}


# 获取用户名
def basic_info():
    url = "https://{}/auth/user-api/login/v1/get-basic-info".format(domain)
    r = requests.get(url=url, headers=headers)
    name = r.json()['data']['name']
    body['userCode'] = name


# 英文用户添加查询，查responseData.data.parentNum
def info_mobile(mobile=None):
    url = "https://{}/bms/user-api/parent/v1/info/mobile?mobile={}".format(domain, mobile)
    r = requests.get(url=url, headers=headers)
    parentNum = r.json()['data']['parentNum']
    return parentNum


# 通过parentNum查stuNum， 路径查responseData.data[0].stuNum
def stu_list(parentNum=None):
    url = "https://{}/bms/user-api/student/v1/studentlist?parentNum={}".format(domain, parentNum)
    r = requests.get(url=url, headers=headers)
    try:
        stuNum = r.json()['data'][0]['stuNum']
        print('stuNum：', stuNum)
    except:
        print('stuNum：未查到')
        stuNum = ""
    return stuNum


# 提交订单
def create_order(stuNum=None, mobile=""):
    url = "https://{}/bms/user-api/order/v1/create".format(domain)
    body['stuNum'] = stuNum
    try:
        r = requests.post(url=url, headers=headers, json=body)
        print("下单结果：", r.json())
    except:
        print(mobile, ': 下单失败')


# 获取班级id, 通过搜索编辑编码获取班级id
def info_class_id():
    url = "https://{}/bms/user-api/classinfo/v1/simple/info".format(domain)
    info_class_body = {"classCode": classCode}
    r = requests.post(url=url, headers=headers, json=info_class_body)
    try:
        classId = r.json()['data']['list'][0]['id']
    except:
        print('下单时未查询到班级：', classCode)
    return classId


def run(mobile=17988880007):
    classId = info_class_id()
    body['classList'][0]['id'] = classId
    parentNum = info_mobile(mobile)
    stuNum = stu_list(parentNum)
    create_order(stuNum, mobile)


# 审核，按手机号查询
def check_order(parent_mobile=17988880007):
    # 查询
    url = "https://{}/bms/user-api/order/v2/check/order-list-page?page=1&size=10&checkStatus=0&minusStatus=&orderChannel=&orderCode=&allData=1&parentMobile={}".format(domain, parent_mobile)
    r1 = requests.get(url=url, headers=check_headers)
    try:
        check_id = r1.json()['data']['list'][0]['id']
    except:
        print("未查到需要审核订单--", parent_mobile)
        return
    check_url = "https://{}/bms/user-api/order/v2/check".format(domain)  # 审核
    check_body = {
        "ids": [
            check_id
        ],
        "checkStatus": 1,
        "checkUserId": 20
    }
    r2 = requests.post(url=check_url, headers=check_headers, json=check_body)
    print("审核结果：", parent_mobile, '---', r2.json())


if __name__ == "__main__":
    for index, item in enumerate(mobile_lst):
        if index <= 1:
            run(item)
            check_order(item)
